package TUILayer;
import ControllerLayer.*;
import java.util.Scanner;



/**
 * Write a description of class ToolStockUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ToolStockUI
{
    private ToolStockController toolStockController;
    public ToolStockUI()
    {
        toolStockController= ToolStockController.getInstance();
    }
    
    public void start()
    {
        toolMenu();
    }
    
    private void toolMenu()
    {
       boolean exit = false;
       while(!exit)
       {
          int choise = writeToolMenu(); 
          if(choise == 1)
          { 
              getTool();
          }
          else{
              if(choise == 2){
                   addTool(); 
              }  
              else{
                  if(choise == 3){
                      deleteTool();
                  }  
                    
                  else
                  {
                      if(choise==4){
                          deleteTools();
                        }
                  
                  
                  else{
                              exit = true;
                        }
                    }
                }
            
       }
    }
    }
    private int writeToolMenu()
    {     
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Product Menu ***");
            System.out.println(" (1) Show information about a tool");
            System.out.println(" (2) Add tools");
            System.out.println(" (3) Delete tool");
            System.out.println(" (4) Delete all the tools with the same barCode");
            System.out.println(" (5) Return");
            System.out.print("\n Make your choice: ");
            
            int choise = keyboard.nextInt();
            
            return choise;
        }
    
    
    private String inputName()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Title:  ");
         String name = keyboard.nextLine();
         return name;
    }
   
    
    private double inputPricePerDay()
    {
        boolean ok = false;
        double price=0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Loaning Price Per Day:  ");
           try{
             price = keyboard.nextDouble();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("It should be a amount of money -  try again");
             String input = keyboard.nextLine();
           }
        }
        return price;
    } 
    
    
     private double inputDeposit()
    {
        boolean ok = false;
        double price=0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Deposit amount of money for the tool:  ");
           try{
             price = keyboard.nextDouble();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("It should be a amount of money -  try again");
             String input = keyboard.nextLine();
           }
        }
        return price;
    } 
    
    
    
    private int inputNumber()
    {
        boolean ok = false;
        int number=0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" How many items of this kind do you add to the tool stock?:  ");
           try{
             number = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("It should be a number");
             String input = keyboard.nextLine();
           }
        }
        return number;
    } 
    
    
    
     private int inputID()
    {
        boolean ok = false;
        int id=0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Tool ID :  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("It should be a number");
             String input = keyboard.nextLine();
           }
        }
        return id;
    } 
   
     
    private String  inputBarCode()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Input item code: ");
        String address = keyboard.nextLine();
        return address;
    }
    
    

    
    
    private void addTool()
    {
        toolStockController.addTool(inputName(), inputBarCode(), inputPricePerDay(),inputDeposit(), inputNumber());
        inputContinue();
    }
    
    
    private void getTool()
    {String barCode=inputBarCode();
        try{
        
        System.out.println(toolStockController.getTool(barCode));
        inputContinue();
    }
        catch(NullPointerException exp)
        {
            System.out.println("There is not such product: "+barCode);
            inputContinue();
        }
    }
    
    private void deleteTools()
    {
        String barCode= inputBarCode();
        try 
        {toolStockController.removeSameTools(barCode);
        System.out.println("Tools have been deleted.");
        }
        catch(NullPointerException exp)
        {
            System.out.println("There are no such tools: "+barCode);
            
        }
        inputContinue();
    }
    
    
    
    private void deleteTool()
    {
        String barCode=inputBarCode();
        int id= inputID();
        try {toolStockController.deleteTool(barCode,id);
        System.out.println("Tool has been deleted.");
        }
        catch(NullPointerException exp)
        {
            System.out.println("There is no such tool");
            
        }
        inputContinue();
    }
        
    
    private void inputContinue()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Press Enter to continue:  ");
         String code = keyboard.nextLine();
    }
    
}
